package com.ruoyi.project.manhua.name.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.project.manhua.name.domain.MhName;
import com.ruoyi.project.manhua.name.service.IMhNameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 漫画名称 Controller
 *
 * @author yuan
 * @date 2021-01-12
 */
@Controller
@RequestMapping("/manhua/name")
public class MhNameController extends BaseController {
    private String prefix = "manhua/name";

    @Autowired
    private IMhNameService mhNameService;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    @RequiresPermissions("manhua:name:view")
    @GetMapping()
    public String name() {
        return prefix + "/name";
    }

    /**
     * 查询漫画名称 列表
     */
    @RequiresPermissions("manhua:name:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MhName mhName) {

        startPage();
        List<MhName> list = mhNameService.selectMhNameList(mhName);

        TableDataInfo dataTable = getDataTable(list);
        /*转换图片 链接*/
        list.forEach((e) -> {
            e.setTheCover(Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getTheCover())));
        });

        return dataTable;
    }

    /**
     * 导出漫画名称 列表
     */
    @RequiresPermissions("manhua:name:export")
    @Log(title = "漫画名称 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MhName mhName) {
        List<MhName> list = mhNameService.selectMhNameList(mhName);
        ExcelUtil<MhName> util = new ExcelUtil<MhName>(MhName.class);
        return util.exportExcel(list, "name");
    }

    /**
     * 新增漫画名称
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存漫画名称
     */
    @RequiresPermissions("manhua:name:add")
    @Log(title = "漫画名称 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MhName mhName, @RequestParam("theCovers") MultipartFile theCovers) {
        return toAjax(mhNameService.insertMhName(mhName, theCovers));
    }

    /**
     * 修改漫画名称
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MhName mhName = mhNameService.selectMhNameById(id);
        mmap.put("mhName", mhName);
        return prefix + "/edit";
    }

    /**
     * 修改保存漫画名称
     */
    @RequiresPermissions("manhua:name:edit")
    @Log(title = "漫画名称 ", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MhName mhName) {
        return toAjax(mhNameService.updateMhName(mhName));
    }

    /**
     * 删除漫画名称
     */
    @RequiresPermissions("manhua:name:remove")
    @Log(title = "漫画名称 ", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(mhNameService.deleteMhNameByIds(ids));
    }


    /**
     * 更换封面
     */
    @RequiresPermissions("manhua:name:edit")
    @Log(title = "更换封面", businessType = BusinessType.DELETE)
    @PostMapping("/tupian")
    @ResponseBody
    public AjaxResult tuPian(@RequestParam("id") Long id, @RequestParam("theCovers") MultipartFile theCovers) {
        return toAjax(mhNameService.tuPian(id, theCovers));
    }


    /**
     * 按照阅读量升序分页查询 前十
     *
     * @return
     */
    @RequiresPermissions("manhua:name:view")
    @Log(title = "更换封面", businessType = BusinessType.DELETE)
    @GetMapping("/getQianWu")
    @ResponseBody
    public AjaxResult getQianWu() {
        return AjaxResult.success(mhNameService.getQianWu());
    }


}
